#include "events_cb.hpp"
#include "xthread_pool.hpp"
#include "xftp_server_cmd.hpp"

#include "xftp_factory.hpp"

#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <glog/logging.h>


void events_cb::listen_cb(struct evconnlistener * e, evutil_socket_t fd, sockaddr *addr, int socklen, void *arg)
{
    
    //thread_task *task = new xftp_server_cmd();
    thread_task *task = xftp_factory::getInstance()->crate_task();
    VLOG(0)<<"create task";
    // 一旦监听到事件的到来，就创建任务对象
    task->sockfd = fd;
    XThreadPool::getInstance()->dispatch(task);

}
void events_cb::write_cb(struct bufferevent *buffer ,void *arg)
{
    VLOG(1)<<"write call back";
    char data[1024] = {0};
    xftp_server_cmd * cmd = reinterpret_cast<xftp_server_cmd*>(arg);
    int len = bufferevent_write(buffer,data,strlen(data));
    if (len <= 0)
    {
        VLOG(3)<<"write error";
        bufferevent_free(buffer);
        delete cmd;
        return;
    }
    
}

void events_cb::event_cb(struct bufferevent *buffer, short what, void *arg)
{
    xftp_server_cmd *cmd = reinterpret_cast<xftp_server_cmd*>(arg);
    cmd->event_cb(buffer,what);
  
    
}
void events_cb::read_cb(struct bufferevent *buffer,void *arg)
{
    VLOG(0)<<"read_cb";
    char data[1024] ={0};
    xftp_server_cmd *cmd = reinterpret_cast<xftp_server_cmd*>(arg);
    cmd->read_cb(buffer);
}